"""
Case Type   : 功能测试
Case Name   : 不支持自治事务返回集合类型,合理报错
Create At   : 2024/7/3
Owner       : peilinqian
Description :
    step1: 创建表;
    step2: 创建包含自治事务的函数返回setof（集合）;
Expect      :
    step1: 创建表; expect：创建成功
    step2: 创建包含自治事务的函数返回setof（集合）;expect:合理报错
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DblinkCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.t_name1 = 't_autonomous_transaction_0038_01'
        self.t_name2 = 't_autonomous_transaction_0038_02'
        self.func_name = 'f_autonomous_transaction_0038'

    def test_main(self):
        step_txt = '----step1: 创建表; expect：创建成功----'
        self.log.info(step_txt)
        sql = f"drop table if exists {self.t_name1};" \
              f"create table {self.t_name1}(id int,a date);" \
              f"drop table if exists {self.t_name2};" \
              f"create table {self.t_name2}(id int,a date);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, msg,
                      '执行失败:' + step_txt)

        step_txt = '----step2: 创建包含自治事务的函数返回setof（集合）;expect:合理报错----; '
        self.log.info(step_txt)
        sql = f'''create or replace function {self.func_name}(num1  int) 
        returns setof {self.t_name1}
        language plpgsql as \$\$
        declare
        count int :=3;
        test_row {self.t_name1}%rowtype;
        pragma autonomous_transaction;
        begin
            while true
            loop
            if count=3 then
            null;
            else
            if count=2 then
                insert into {self.t_name2} values (count,'2021-03-03');
                goto pos1;
            end if;
            end if;
            count=count-1;
            end loop;
            insert into {self.t_name2} values (1000,'2021-04-04');
             <<pos1>>
             for test_row in select * from {self.t_name2}
             loop
                return next test_row;
               end loop;
              return;
        end;
        \$\$
        '''
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        err_flag = 'ERROR:  Autonomous transactions do not support RETURN SETOF'
        self.assertIn(err_flag, msg, '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----删除表，恢复参数; expect：成功----'
        self.log.info(step_txt)
        sql = f"drop table if exists {self.t_name1};" \
              f"drop table if exists {self.t_name2};"
        msg1 = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg1)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, msg1,
                      '执行失败:' + step_txt)
